package com.junit;

import com.hcfly.utils.ESClient;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.action.admin.indices.get.*;
import org.junit.Test;

import java.io.IOException;

import static org.elasticsearch.common.settings.Settings.*;

public class CreateIndexDemo {
    private static final RestHighLevelClient client = ESClient.getClient();
    private static final String index = "person";
    private static final String type  = "man";
    
    @Test
    public void deleteIndex() throws IOException {
        /**
         * 删除索引
         */
        DeleteIndexRequest request = new DeleteIndexRequest().indices(index);
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete);
    }
    
    @Test
    public void createIndex() throws IOException {
        /**
         * 构建索引
         *
         * PUT /person
         * {
         *   "settings": {
         *     "number_of_shards": 5,
         *     "number_of_replicas": 1
         *   },
         *   "mappings": {
         *     "man":{
         *       "properties":{
         *         "name":{
         *           "type": "text"
         *         },
         *
         *         "age":{
         *           "type": "integer"
         *         },
         *         "birthday":{
         *           "type":"date",
         *           "format": "yyyy-MM-dd"
         *         }
         *       }
         *     }
         *   }
         * }
         */

        // 1，准备索引的setting
        Settings.Builder settings = Settings.builder()
                .put("number_of_shards", 5)
                .put("number_of_replicas", 1);

        // 2,准备索引结构的mapping
        XContentBuilder mapping = JsonXContent.contentBuilder()
                .startObject()
                    .startObject("properties")
                        .startObject("name")
                            .field("type", "text")
                        .endObject()
                        .startObject("age")
                            .field("type", "integer")
                        .endObject()
                        .startObject("birthday")
                            .field("type", "date")
                            .field("format", "yyyy-MM-dd")
                        .endObject()
                    .endObject()
                .endObject();

        // 3,将setting和mapping封装成一个Request对象
        CreateIndexRequest request = new CreateIndexRequest(index)
                .settings(settings)
                .mapping(type, mapping);

        // 4, 通过client对象去连接ES并执行创建语句
        CreateIndexResponse resp = client.indices().create(request, RequestOptions.DEFAULT);
        // 5, 输出
        System.out.println("Resp: " + resp.toString());



    }

    @Test
    public void indexExists() throws IOException{
        //准备request对象
        GetIndexRequest request = new GetIndexRequest();
        request.indices(index);

        //通过client去操作
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

        System.out.println(exists);

    }
}
